package com.sky.service.impl;/*
@author sun
@date 24/07/15
@project IntelliJ IDEA
*/

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    // 新增菜品 dishMapper增加1 或 dishFlavorMapper 批量增加n条
    @Transactional
    public void addDishWithFlavor(DishDTO dishdto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishdto, dish);
        dishMapper.insertOne(dish);

        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishdto.getFlavors();
        if (flavors != null&& flavors.size()>0){
            flavors.forEach(dishFlavor->{
                dishFlavor.setDishId(dishId);
            });
            dishFlavorMapper.insertAll(flavors);
        }
    }

    // 菜品分页查询
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {

        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<Dish> page= dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());

    }

    // 菜品批量删除
    public void deleteList(List<Long> ids) {
        // 循环遍历 是否有起售中的菜品
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        // 起售中的是否被套餐关联了
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        // 删除菜品表中的菜品数据(主表数据、关联数据)
        for (Long id : ids) {
            dishMapper.delById(id); // 删除主表数据
            dishFlavorMapper.delByDishId(id); // 删除关联数据
        }
    }

    // 根据id查询菜品和对应的口味数据
    public DishVO getByIdWithFlavor(Long id) {

        Dish dish = dishMapper.getById(id); // 根据id查询菜品数据
        List<DishFlavor> dishWithFlavorById = dishFlavorMapper.getByDishId(id); // 根据菜品id去查口味数据

        DishVO dishVO = new DishVO(); // 封装数据 得到返回数据
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishWithFlavorById);

        return dishVO;
    }

    // 修改菜品 // 先更新基本信息 根据dishDTO的id然后删除原有的信息 重新批量插入批量数据
    @Transactional
    public void updateDishWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish); // 先更新基本信息

        dishFlavorMapper.delByDishId(dishDTO.getId()); // 根据dishDTO的id然后删除原有的信息

        List<DishFlavor> flavors = dishDTO.getFlavors(); // 重新批量插入批量数据
        if (flavors != null&& flavors.size()>0){
            flavors.forEach(dishFlavor->{
                dishFlavor.setDishId(dishDTO.getId());
            });
            dishFlavorMapper.insertAll(flavors);
        }
    }

    // 根据分类id查询菜品
    public List<Dish> queryCategoryById(Long categoryId) {
        Dish dish = new Dish().builder().categoryId(categoryId).status(StatusConstant.ENABLE).build();
        return dishMapper.list(dish);
    }

    // TODO 菜品起售停售 有问题啊
    @Transactional
    public void startOrStop(Integer status, Long id) {
        // 先全部更新
        Dish dish = new Dish().builder().id(id).status(status).build();
        dishMapper.update(dish);

        // 如果是停售操作，还需要将包含当前菜品的套餐也停售
        if (status == StatusConstant.DISABLE){

            List<Long> dishIds = new ArrayList();
            dishIds.add(id);
            List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(dishIds);

            if (setmealIds != null && setmealIds.size()>0){
                for (Long l : setmealIds) {
                    Setmeal setmeal = new Setmeal().builder().id(l).status(StatusConstant.DISABLE).build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }
}
